package com.l2jserver.gameserver.model.zone.type;

import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.model.L2Object.InstanceType;
import com.l2jserver.gameserver.model.L2Skill;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.zone.L2SpawnZone;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;

/**
 * An Custom PvP Zone
 *
 * @author  NeverMore
 */
public class L2TzPvPZone extends L2SpawnZone
{
	private boolean _giveNobleBlessing;
	private boolean _removeNobleBlessing;
	private boolean _earnPvPPonts;
	private boolean _canSummonFriends;
	private boolean _giveBarrierOnEnter;
	private boolean _giveBarrierOnRevive;
	L2Skill _NoblesseBlessing;
	L2Skill _CelestialShield;
	
	public L2TzPvPZone(final int id)
	{
		super(id);
		_giveNobleBlessing = false;
		_removeNobleBlessing = false;
		_earnPvPPonts = true;
		_canSummonFriends = true;
		_giveBarrierOnEnter = false;
		_giveBarrierOnRevive = false;
		setTargetType(InstanceType.L2Playable); // default only playabale
		_NoblesseBlessing = SkillTable.getInstance().getInfo(1323, 1);
		_CelestialShield = SkillTable.getInstance().getInfo(1418, 1);
	}
	
	@Override
	public void setParameter(final String name, final String value)
	{
		if (name.equals("AutoGiveNobles"))
			_giveNobleBlessing = Boolean.parseBoolean(value);
		else if (name.equals("AutoRemoveNobles"))
			_removeNobleBlessing = Boolean.parseBoolean(value);
		else if (name.equals("EarnPvP"))
			_earnPvPPonts = Boolean.parseBoolean(value);
		else if (name.equals("AllowSummonFriend"))
			_canSummonFriends = Boolean.parseBoolean(value);
		else if (name.equals("GiveBarrierOnEnter"))
			_giveBarrierOnEnter = Boolean.parseBoolean(value);
		else if (name.equals("GiveBarrierOnRevive"))
			_giveBarrierOnRevive = Boolean.parseBoolean(value);
		else
			super.setParameter(name, value);
	}
	
	@Override
	protected void onEnter(final L2Character character)
	{
		if (character instanceof L2PcInstance)
		{
			character.setInsideZone(L2Character.ZONE_TZ_PVP, true);
			if (!_canSummonFriends)
				character.setInsideZone(L2Character.ZONE_NOSUMMONFRIEND, true);
			if (!_earnPvPPonts)
				character.setInsideZone(L2Character.ZONE_PVP, true);
			if (character instanceof L2PcInstance)
			{
				final L2PcInstance activechar = (L2PcInstance) character;
				if (!activechar.isNoblesseBlessed() && _giveNobleBlessing)
					_NoblesseBlessing.getEffects(activechar, activechar);
				if (_giveBarrierOnEnter)
					_CelestialShield.getEffects(activechar, activechar);
				activechar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.ENTERED_COMBAT_ZONE));
				activechar.updatePvPFlag(1);
			}
		}
	}
	
	@Override
	protected void onExit(final L2Character character)
	{
		if (character instanceof L2PcInstance)
		{
			character.setInsideZone(L2Character.ZONE_TZ_PVP, false);
			if (!_canSummonFriends)
				character.setInsideZone(L2Character.ZONE_NOSUMMONFRIEND, false);
			if (!_earnPvPPonts)
				character.setInsideZone(L2Character.ZONE_PVP, false);
			if (character instanceof L2PcInstance)
			{
				if (((L2PcInstance) character).isNoblesseBlessed() && _giveNobleBlessing && _removeNobleBlessing)
					((L2PcInstance) character).stopNoblesseBlessing(null);
				
				((L2PcInstance) character).sendPacket(SystemMessage.getSystemMessage(SystemMessageId.LEFT_COMBAT_ZONE));
				((L2PcInstance) character).updatePvPFlag(5);
			}
		}
	}
	
	@Override
	public void onDieInside(final L2Character character)
	{
	}
	
	@Override
	public void onReviveInside(final L2Character character)
	{
		if (character instanceof L2PcInstance)
		{
			final L2PcInstance activechar = (L2PcInstance) character;
			if (!activechar.isNoblesseBlessed() && _giveNobleBlessing)
				_NoblesseBlessing.getEffects(activechar, activechar);
			if (_giveBarrierOnRevive)
				_CelestialShield.getEffects(activechar, activechar);
			
			activechar.updatePvPFlag(1);
		}
	}
}
